using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery;
using System.Net.Sockets;
using System.Threading;

namespace SerialPortService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SerialPortService localService;
        string chat;
        string status;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            lblStatus.Text = "Stopped";
            try
            {
                localService = new SerialPortService();
                localService.NewClientConnected +=
                    new EventHandler(localService_NewClientConnected);
                localService.ClientDisconnected +=
                    new EventHandler(localService_ClientDisconnected);
                localService.DataSentToClient +=
                    new SerialPortService.SerialDataEventHandler(
                    localService_DataSentToClient);
                localService.DataReceivedFromClient +=
                    new SerialPortService.SerialDataEventHandler(
                    localService_DataReceivedFromClient);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to create service." + ex.Message, "SerialPortService",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.Cursor = Cursors.Default;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                localService.Register();
                lblStatus.Text = "Listening";
                btnStart.Enabled = false;
                btnStop.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to unregister service." + ex.Message,
                    "Serial Port Service", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.Cursor = Cursors.Default;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                localService.UnRegister();
                lblStatus.Text = "Stopped";
                btnStart.Enabled = true;
                btnStop.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to unregister service. " + Environment.NewLine +
                    "Error details: " + ex.Message, "Serial Port Service",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.Cursor = Cursors.Default;
        }

        void localService_NewClientConnected(object sender, EventArgs e)
        {
            status = "Connected";
            this.Invoke(new EventHandler(SetStatus));

            chat = "New client connected: " + Environment.NewLine;
            this.Invoke(new EventHandler(SetChat));
        }

        void localService_DataSentToClient(object sender, SerialDataEventArgs e)
        {
            chat = "Server: " + e.Data.Trim() + Environment.NewLine;
            this.Invoke(new EventHandler(SetChat));
        }

        void localService_DataReceivedFromClient(object sender, SerialDataEventArgs e)
        {
            chat = "Client: " + e.Data.Trim() + Environment.NewLine;
            this.Invoke(new EventHandler(SetChat));
        }

        void localService_ClientDisconnected(object sender, EventArgs e)
        {
            status = "Listening";
            this.Invoke(new EventHandler(SetStatus));

            chat = "Client disconnected." + Environment.NewLine + Environment.NewLine;
            this.Invoke(new EventHandler(SetChat));
        }

        private void SetStatus(object sender, EventArgs e)
        {
            lblStatus.Text = status;
            status = "";
        }

        private void SetChat(object sender, EventArgs e)
        {
            lblChat.Text += chat;
            chat = "";
        }
    }
}